package com.tripleying.dogend.dnfipm.controller;

import com.jfoenix.controls.JFXSnackbar;
import com.jfoenix.controls.JFXSnackbar.SnackbarEvent;
import com.jfoenix.controls.JFXTextField;
import com.tripleying.dogend.dnfipm.App;
import com.tripleying.dogend.dnfipm.util.FileUtil;
import java.io.File;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

/**
 * 选择补丁目录页面
 * @author Dogend
 */
public class SelectController {
    
    private double x; 
    private double y;
    private double x_stage;
    private double y_stage;
    private final Stage stage;
    // 错误提示
    private final Label errorLabel;
    private static SelectController sc;
    @FXML
    private JFXSnackbar sb;
    // 补丁目录框
    @FXML
    private JFXTextField folderField;
    
    public SelectController(){
        sc = this;
        stage = App.getStage();
        errorLabel = new Label();
        errorLabel.setFont(new Font(20));
        errorLabel.setTextFill(Color.WHITE);
        errorLabel.setPrefHeight(50);
        errorLabel.setPrefWidth(680);
        errorLabel.setTextAlignment(TextAlignment.CENTER);
        errorLabel.setBackground(new Background(new BackgroundFill(Color.BLACK,null,null)));
    }
    
    /**
     * 选择文件夹
     */
    @FXML
    private void selectFolder(){
        DirectoryChooser fileChooser = new DirectoryChooser();
        if(folderField.getText().trim().length()>0){
            File dir = new File(folderField.getText().trim());
            if(dir.exists()) fileChooser.setInitialDirectory(new File(folderField.getText()));
        }
        fileChooser.setTitle("选择DNF目录下的ImagePacks2文件夹");
        File file = fileChooser.showDialog(stage);
        if(file!=null){
            folderField.setText(file.getAbsolutePath());
        }
    }
    
    /**
     * 保存文件夹到配置文件中
     * @throws IOException 文件操作异常
     */
    @FXML
    private void saveFolder() throws IOException{
        String text = folderField.getText().trim();
        String path = text;
        if(path.endsWith("/")) path = path.substring(0, path.length()-1);
        if(path.endsWith("ImagePacks2") && new File(text).exists()){
            if(FileUtil.saveConfig("dnf", text)){
                stage.hide();
                App.setFolder(new File(text));
                App.switchMain();
            }else{
                sb.close();
                errorLabel.setText("保存配置文件失败");
                sb.enqueue(new SnackbarEvent(errorLabel));
            }
        }else{
            sb.close();
            errorLabel.setText("路径错误请重新选择");
            sb.enqueue(new SnackbarEvent(errorLabel));
        }
    }
    
    @FXML
    private void press(MouseEvent e){
        x = e.getScreenX();
	y = e.getScreenY();
        x_stage = stage.getX();
        y_stage = stage.getY();
    }
    
    @FXML
    private void drag(MouseEvent e){
        stage.setX(x_stage + e.getScreenX() - x);
        if(y_stage + e.getScreenY() - y < 0) {
            stage.setY(y_stage);
        }else {
            stage.setY(y_stage + e.getScreenY() - y);
        }
    }

    @FXML
    private void exit(){
        System.exit(0);
    }
    
    /**
     * 设置目录框中初始的文件路径
     * @param path 路径
     */
    public void setInitFolder(String path){
        folderField.setText(path);
    }

    public static SelectController getInstance() {
        return sc;
    }
    
}
